﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace BlogOutModell
{
    public class CommentDB
    {
        private static readonly BlogOutModelContainer Context = DB.Context;

        public int AddComment(Comment comment)
        {
            Context.Comments.Add(comment);
            Context.SaveChanges();
            return comment.Id;
        }

        public Comment GetComment(int id)
        {
            Comment result = Context.Comments.SingleOrDefault(c => c.Id == id && !c.IsDeleted);
            return result;
        }

        public List<Comment> GetAllNotDeletedCommentsByPostId(int postId)
        {
            List<Comment> result = Context.Comments.Where(c => c.PostId == postId && !c.IsDeleted).ToList();
            return result;
        }

        public void DeleteComment(int id)
        {
            Comment comment = GetComment(id);
            comment.IsDeleted = true;
            Context.SaveChanges();
        }
    }
}